package org.demo2;

import java.lang.reflect.Field;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        //class类中用于获取成员变量的方法
        //1.先获取class字节码文件的对象
        Class clazz = Class.forName("org.demo2.student");
        //2.获取成员变量的对象
        //(1)
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        System.out.println();

        //(2)
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }

        System.out.println();

        //(3)
        Field gender = clazz.getField("gender");//只有public才行
        System.out.println(gender);
        int modifiers = gender.getModifiers();//获取字段的权限修饰符
        System.out.println(modifiers);
        String name = gender.getName();//获取字段名
        System.out.println(name);
        Class<?> type = gender.getType();//获取数据类型
        System.out.println(type);
        student student = new student("男","张三",23);
        Object o = gender.get(student);//获取指定字段的值
        System.out.println(o);
        gender.set(student,"女");
        System.out.println(student);


        System.out.println();

        //(4)


    }
}